## 纹理处理工具类
class_name TextureUtils

enum ImageType{JPG,PNG,WEBP}   # 支持存储的图片类型

## 直接从路径加载图片并返回ImageTexture
static func load_texture(img_path:String) -> ImageTexture:
	var texture = ImageTexture.new()
	var img = Image.load_from_file(img_path)
	texture = texture.create_from_image(img)
	return texture

## 水平翻转Texture2D
static func flip_x_texture(texture:Texture2D) -> ImageTexture:
	var new_texture = ImageTexture.new()
	var img = texture.get_image()
	img.flip_x()
	new_texture = new_texture.create_from_image(img)
	return new_texture

## 垂直翻转Texture2D
static func flip_y_texture(texture:Texture2D) -> ImageTexture:
	var new_texture = ImageTexture.new()
	var img = texture.get_image()
	img.flip_y()
	new_texture = new_texture.create_from_image(img)
	return new_texture
	
## 创建并返回原图片的灰度图
static func gray_scale_texture(texture:Texture2D) -> ImageTexture:
	var new_texture = ImageTexture.new()
	var img = texture.get_image()
	for x in range(img.get_width()):
		for y in range(img.get_height()):
			var old_color = img.get_pixel(x,y)
			var gray = (old_color.r +  old_color.g +  old_color.b)/3
			var new_color = Color(gray,gray,gray,old_color.a)
			img.set_pixel(x,y,new_color)
	new_texture = new_texture.create_from_image(img)
	return new_texture

## 获取并返回Texture2D的一个局部矩形区域
static func get_region(texture:Texture2D,region: Rect2i) -> ImageTexture:
	var new_texture = ImageTexture.new()
	var img = texture.get_image()
	img = img.get_region(region)
	new_texture = new_texture.create_from_image(img)
	return new_texture

## 为图片添加边框
static func draw_border(texture:Texture2D,border_size:PackedInt32Array = [10,10,10,10],border_color:Color=Color.WHITE) -> ImageTexture:
	var new_texture = ImageTexture.new()
	# 1.通过原图和边框尺寸计算新图的尺寸
	var old_img = texture.get_image() # 原图片
	var new_img = Image.new()         # 新图片
	# 新的尺寸 = 原图片尺寸 + 边框尺寸
	var new_size = old_img.get_size() + Vector2i(border_size[1] + border_size[3],border_size[0] + border_size[2])
	
	# 2.通过新尺寸构造新的Image
	new_img = new_img.create(new_size.x,new_size.y,false,Image.FORMAT_RGBA8)
	new_img.fill(border_color)  # 填充整个纹理区域为边框颜色
	
	# 3.在新图的指定位置绘制原图
	var rect = old_img.get_used_rect()                # 原图的纹理区域
	var pos = Vector2i(border_size[3],border_size[0]) # 起始位置
	new_img.blit_rect(old_img,rect,pos)               # 将原图绘制到新Image的指定区域
	
	# 4.通过新Image构造ImageTexture
	new_texture = new_texture.create_from_image(new_img)
	return new_texture

## 创建纯色块的ImageTexture
static func color_block(size:Vector2,color:Color=Color.WHITE) -> ImageTexture:
	var new_texture = ImageTexture.new()
	var img = Image.new()
	img = img.create(size.x,size.y,false,Image.FORMAT_RGBA8)
	img.fill(color)
	new_texture = new_texture.create_from_image(img)
	return new_texture

## 创建棋盘格的ImageTexture
static func checker_board(img_size:Vector2i,cell_size:Vector2i=Vector2i(10,10),color1:Color=Color.WHITE,color2:Color=Color.BLACK) -> ImageTexture:
	var new_texture = ImageTexture.new()
	var img = Image.new()
	img = img.create(img_size.x,img_size.y,false,Image.FORMAT_RGBA8)
	# 计算所需要填充的数量
	var grid_size = ceil(img_size / cell_size) 
	for x in range(grid_size.x):
		for y in range(grid_size.y):
			var rect = Rect2i(cell_size * Vector2i(x,y),cell_size)
			if (x % 2 == 0 and y % 2 == 0) or (x % 2 == 1 and y % 2 == 1): # 奇数行奇数列，或者偶数行偶数列
				img.fill_rect(rect,color1)
			else:
				img.fill_rect(rect,color2)
	new_texture = new_texture.create_from_image(img)
	return new_texture

## 直接将Texture2D保存为本地图片文件
static func save_texture(texture:Texture2D,file_path:String,img_type:ImageType=ImageType.JPG) -> void:
	var img = texture.get_image()
	match img_type:
		ImageType.JPG:
			img.save_jpg(file_path)
		ImageType.PNG:
			img.save_png(file_path)
		ImageType.WEBP:
			img.save_webp(file_path)


## 生成Image的缩略图
static func create_thumb(img:Image,size:Vector2) -> ImageTexture:
	var texture = ImageTexture.new()
	var old_size = img.get_size()  # 原图尺寸
	var aspect_ratio = old_size.aspect()
	
	# 原图比缩略图宽或高时
	if old_size.x > size.x or old_size.y > size.y:
		if old_size.x > old_size.y: # 图片水平方向比较长
			var new_w = size.x
			var new_h = size.x / aspect_ratio
			img.resize(new_w,new_h)
		else:
			var new_h = size.y 
			var new_w = size.y * aspect_ratio
			img.resize(new_w,new_h,Image.INTERPOLATE_LANCZOS)
	texture = texture.create_from_image(img)
	return texture

## 生成指定路径文件的缩略图
static func create_thumb_from_file(img_path:String,size:Vector2) -> ImageTexture:
	var texture = ImageTexture.new()
	if img_path.get_extension() in ["png","jpg","svg"]:
		var img = Image.load_from_file(img_path)
		if img:
			texture = create_thumb(img,size)
		return texture
	else:
		return null
